package com.wc.算法提高课.E第五章_数学知识.矩阵乘法.佳佳的斐波那契;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/7 22:41
 * @description https://www.acwing.com/problem/content/1306/
 */
public class Main {
    /**
     * 思路：<p>
     * 矩阵乘法 + 快速幂<p>
     * T[n] = F[1] + 2F[2] + ... + nF[n]<p>
     * S[n] = F[1] + F[2] + ... + F[n] <p>
     * nS[n] = nF[1] + nF[2] + ... + nF[n] <p>
     * nS[n] - T[n] = (n - 1)F[1] + (n - 2)F[2] + ... + F[n - 1] => ①<p>
     * (n + 1)S[n + 1] - T[n + 1] = nF[1] + (n - 1)F[2] + ... 2F[n - 1] + F[n] => ②<p>
     * ② - ① = F[1] + F[2] + ... + F[n] = S[n]<p>
     * P[n] = nS[n] - T[n]<p>
     * P[n] - P[n - 1] = S[n - 1]<p>
     * P[n] = P[n - 1] + S[n - 1]<p>
     * F[n] = {f[n], f[n + 1], S[n], P[n]}<p>
     * F[n + 1] = {f[n + 1], f[n + 2], S[n + 1], P[n + 1]}<p>
     * F[n] * A = F[n + 1]<p>
     * A = {<p>
     * {0, 1, 0, 0},<p>
     * {1, 1, 1, 0},<p>
     * {0, 0, 1, 1},<p>
     * {0, 0, 0, 1}<p>
     * }<p>
     * T[n] = nS[n] - P[n]<p>
     * F[0] = {0, 1, 0, 0}
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 4;
    static int n, p;

    public static void main(String[] args) {
        n = sc.nextInt();
        p = sc.nextInt();
        long[][] f = {
                {0, 1, 0, 0},
                {0, 0, 0, 0},
                {0, 0, 0, 0},
                {0, 0, 0, 0}
        };
        long[][] a = {
                {0, 1, 0, 0},
                {1, 1, 1, 0},
                {0, 0, 1, 1},
                {0, 0, 0, 1}
        };
        a = qmi(a, n);
        mul(f, f, a);
        out.println(((n * f[0][2] - f[0][3]) % p + p) % p);
        out.flush();
    }

    static void mul(long[][] c, long[][] a, long[][] b) {
        long[][] res = new long[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                for (int k = 0; k < N; k++) {
                    res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % p;
                }
            }
        }
        for (int i = 0; i < N; i++) {
            System.arraycopy(res[i], 0, c[i], 0, N);
        }
    }

    static long[][] qmi(long[][] a, int k) {
        long[][] res = getE();
        while (k > 0) {
            if ((k & 1) == 1) mul(res, res, a);
            mul(a, a, a);
            k >>= 1;
        }
        return res;
    }

    static long[][] getE() {
        long[][] E = new long[N][N];
        for (int i = 0; i < N; i++) E[i][i] = 1;
        return E;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
